package com.gitee.hljdrl.dsbridge.client;

import android.graphics.Bitmap;
import android.net.http.SslError;
import android.text.TextUtils;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;

import com.ffsky.logger.L;
import com.gitee.hljdrl.dsbridge.basis.DSViewEvent;

public class DSWebViewClient extends WebViewClient {


    protected String TAG = "DSWebViewClient";


    private DSViewEvent mDSViewEvent;

    public DSWebViewClient() {

    }

    public DSWebViewClient(DSViewEvent dsViewEvent) {
        this.mDSViewEvent = dsViewEvent;
    }


    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if(mDSViewEvent!=null){
            mDSViewEvent.hideLoading();
        }
    }


    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        if(mDSViewEvent!=null){
            mDSViewEvent.showLoading();
        }
    }


    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        L.i(TAG, "onReceivedError-->errorCode=", errorCode);
        L.i(TAG, "onReceivedError-->description=", description);
        L.i(TAG, "onReceivedError-->failingUrl=", failingUrl);

        if (errorCode == WebViewClient.ERROR_HOST_LOOKUP || errorCode == WebViewClient.ERROR_CONNECT
                || TextUtils.equals(description, "net::ERR_NAME_NOT_RESOLVED")
                || TextUtils.equals(description, "net::ERR_CONNECTION_ABORTED")) {
            if (!TextUtils.isEmpty(failingUrl)) {
                if(mDSViewEvent!=null){
                    mDSViewEvent.showError();
                    mDSViewEvent.hideLoading();
                }
                return;
            }
        }
        if ((errorCode == ERROR_TIMEOUT || errorCode == ERROR_CONNECT) && TextUtils.equals(description, "net:ERR_CONNECTION_RESET")) {
//            if (retryCount < MAX_RETRY) {
//                retryCount++; // 增加重试计数
//                if (mWebView != null) {
//                    mWebView.postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            mWebView.loadUrl(failingUrl);
//                        }
//                    }, 2000);
//                }
//                return;
//            } else {
//                // 可以在这里处理重试后仍然失败的情况，例如显示错误页面
//                retryCount = 0; // 重置重试计数
//            }
            //
            if (!TextUtils.isEmpty(failingUrl)) {
                if(mDSViewEvent!=null){
                    mDSViewEvent.showError();
                    mDSViewEvent.hideLoading();
                }
                return;
            }
        }
    }



    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }


    @Nullable
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        return super.shouldInterceptRequest(view, request);
    }


    @Nullable
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        return super.shouldInterceptRequest(view, url);
    }
}
